/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package riteservices.services;

import java.util.ArrayList;
import java.util.List;
import riteservices.model.bean.Fbfriends;
import riteservices.model.bean.Round;
import riteservices.model.bean.User;
import riteservices.model.dao.FbFriendsDAO;
import riteservices.model.dao.RoundDAO;
import riteservices.util.Constantes;

/**
 *
 * @author sixviv
 */
public class RoundSrv {
    private RoundDAO roundDAO = new RoundDAO();
    private RouteSrv routesrv= new RouteSrv();
    private FbFriendsDAO fbFriendsDAO = new FbFriendsDAO();
    
    public String createRound(Round round){
        if(roundDAO.add(round)){
        
            return Constantes.TRUE_STR;
        }else{
            return Constantes.FALSE_STR;
        }
    }
    
    public Round getById(int id){
        return roundDAO.getRoundById(id);
    }

    public String updateRound(Round round){
        String mensaje="";
        if(roundDAO.update(round)){
            mensaje=Constantes.TRUE_STR;
        }else{
            mensaje=Constantes.FALSE_STR;
        }

    return mensaje;
    }
    public String deleteRound(Round round){
        String mensaje="";
        if(roundDAO.delete(round)){
        mensaje=routesrv.deleteRoute(round.getRoute());
        }else{
        mensaje=Constantes.FALSE_STR;
        }
    return mensaje;
    }
    
    public List<Round> getRoundsByOwner(Integer ownerId)
    {
        return roundDAO.getRoundsByOwner(ownerId);
    }

    public Round getRoundDetails(int roundid){
    return roundDAO.getRoundById(roundid);
    }

    public List<Round> searchInPublicRoundsDestiny(String destiny,String schedule, User user){
        List<Round> rounds = new ArrayList<Round>();
        List<Fbfriends> fbfriends =  fbFriendsDAO.getFriends(user.getIdUser());
        for(Fbfriends fbfriend : fbfriends){
            rounds.addAll(this.roundDAO.getPublicRoundsFriend(destiny, schedule, fbfriend));
        }
        List<Round> publicRounds = new ArrayList<Round>();
        publicRounds = roundDAO.getPublicRounds(destiny, schedule, user);
        for(Round round :publicRounds){
            if(!containsRound(round, rounds)){
                rounds.add(round);
            }
        }
        return rounds;
    }
    
    public List<Round> searchInPublicRoundsOrigin(String origin,String schedule, User user){
        List<Round> rounds = new ArrayList<Round>();
        List<Fbfriends> fbfriends =  fbFriendsDAO.getFriends(user.getIdUser());
        for(Fbfriends fbfriend : fbfriends){
            rounds.addAll(this.roundDAO.getPublicRoundsFriendOrigin(origin, schedule, fbfriend));
        }
        List<Round> publicRounds = new ArrayList<Round>();
        publicRounds = roundDAO.getPublicRoundsOrigin(origin, schedule, user);
        for(Round round :publicRounds){
            if(!containsRound(round, rounds)){
                rounds.add(round);
            }
        }
        return rounds;
    }
    
    private boolean containsRound(Round roundComp, List<Round> rounds){
        boolean band = false;
        for(Round round: rounds){
            if(roundComp.getIdRound().compareTo(round.getIdRound())==0){
                band = true;
            }
        }
        return band;
    }
    

}
